//
//  HMFeaure.swift
//  swift一遍敲
//
//  Created by 刘耀宗 on 16/4/20.
//  Copyright © 2016年 刘耀宗. All rights reserved.
//

import UIKit
import SnapKit

class HMFeaure: UICollectionViewController{
    let  ReuseIdentifier = "ReuseIdentifier"
    let flowLayout = UICollectionViewFlowLayout()
    init() {
        
        super.init(collectionViewLayout: flowLayout)
    
    }
    
        
  
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
      
        setLayout()
        

        //注册
        self.collectionView?.registerClass(HMCollectionCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
        
        
        
    }
    private func setLayout()
    {
        
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        
        //水平滚动
        flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        flowLayout.itemSize = UIScreen.mainScreen().bounds.size
        //分页
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
    
    }
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! HMCollectionCell
        //图片名称
        imageName = "new_feature_" + "\(indexPath.row + 1)"
        
        cell.imageName = imageName
        
        return cell
        
    }
    var imageName: String?
    

    //滚动结束的
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.size.width)
        
        if index == 3
        {
          
            //获取当前得cell
             let indexPath = NSIndexPath(forItem: index, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! HMCollectionCell
                cell.startAnimation()
        
        
        }
        
    }
}
